package database.service;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import database.auxiliary.LogGenerator;

import po.BookInventoryPO;

public class BookInventoryDataServiceSerializableImpl implements
		BookInventoryDataService {

	private ArrayList<BookInventoryPO> bookInventoryList;
	 
	public  BookInventoryDataServiceSerializableImpl(){
		bookInventoryList=new ArrayList<BookInventoryPO>();
	}

	@Override
	public void load() {
		try {
			File file = new File("data/book inventory.ser");
			if (!file.exists()) {
				file.createNewFile();
			}
			ObjectInputStream oiStream = new ObjectInputStream(
					new FileInputStream(file));
			BookInventoryPO bookInventoryPO;
			while ((bookInventoryPO = (BookInventoryPO) oiStream.readObject()) != null) {
				bookInventoryList.add(bookInventoryPO);
			}
			LogGenerator.log("Book inventory data load succeed");
		} catch (EOFException e) {
			save();
			load();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void save() {
		try {
			FileOutputStream fos = new FileOutputStream("data/book inventory.ser");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			for (int i = 0; i < bookInventoryList.size(); i++) {
				oos.writeObject(bookInventoryList.get(i));
			}
			oos.writeObject(null);
			oos.close();
			LogGenerator.log("Book inventory data save succeed");
		} catch (FileNotFoundException e) {
			System.out.println("File found failed!");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public Iterator<BookInventoryPO> getIterator() {
		LogGenerator.log("Book inventory return itr, length = " + bookInventoryList.size());
		return bookInventoryList.iterator();
	}

	@Override
	public int insert(BookInventoryPO bookInventory) {
		bookInventoryList.add(bookInventory);
		LogGenerator.log("Book inventory insert: " + bookInventory.toString());
		return bookInventory.getBookID();
	}

	@Override
	public void delete(BookInventoryPO bookInventory) {
		for (int i = 0; i < bookInventoryList.size(); i++) {
			if (bookInventoryList.get(i) == bookInventory) {
				bookInventoryList.get(i).setBookID(-bookInventoryList.get(i).getBookID());
				LogGenerator.log("Book inventory delete: " + bookInventory);
			}
		}
	}

	@Override
	public void update(BookInventoryPO bookInventory) {
		for (int i = 0; i < bookInventoryList.size(); i++) {
			if (bookInventory.getBookID() == bookInventoryList.get(i).getBookID()) {
				bookInventoryList.set(i, bookInventory);
				LogGenerator.log("Book inventory update: " + bookInventory);
			}
		}
	}

	@Override
	public BookInventoryPO find(int id) {
		for (int i = 0; i < bookInventoryList.size(); i++) {
			if (id == bookInventoryList.get(i).getBookID()) {
				LogGenerator.log("Book inventory find: " + bookInventoryList.get(i));
				return new BookInventoryPO(bookInventoryList.get(i));
			}
		}
      
		return null;
	}

}
